$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeperRope.html";
    });
    //点击 取消
    $(".foot .del_receive").on("click", function () {
        window.location.href = "storekeeperRope.html";
    });

    //批号   获取 焦点
    $("input#goods_id").focus();

    //根据扫描内容 得到 信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描产品标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            loading_show("搜索中...");
            //获取工单
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/pallet/getWarehouseForWeigh',
                data: {
                    batch: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $("#goods_id").val("");
                        //console.log(data);
                        $alert(val + "获取成功", 1000);
                        $(".content .id").val(data._id);
                        $(".content .batch").text(data.batch);
                        $(".content .cpmc").text(data.inventoryName);
                        $(".content .cpgg").text(data.inventorySpecification);
                        $(".content .length").text(data.availableLength);
                        $(".content .orderLength").text(data.orderLength);
                        $(".content .breakpoint").text(data.breakpoint || "");
                        $(".content .segmentLength").text(data.segmentLength);
                        $(".content input").val("");
                        $(".content input.mz").focus();
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        var batch = $(".content .batch").text();
        //判断数据
        if (!batch) {
            $alert("请扫描钢丝绳产品标签！");
            pass = 1;
            return;
        } else if (!$("input.mz").val()) {
            $alert("请输入毛重！");
            pass = 1;
            return;
        } else if (!$("input.pz").val()) {
            $alert("请输入皮重！");
            pass = 1;
            return;
        } else if (Number($("input.pz").val()) >= Number($("input.mz").val())) {
            $alert("皮重应小于毛重！");
            pass = 1;
            return;
        }
        //获取 信息
        loading_show("提交中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/pallet/warehouseWeigh',
            data: {
                _id: $(".content .id").val(),
                batch: batch,
                grossWeight: $("input.mz").val(),
                tareWeight: $("input.pz").val(),
                weight: $("input.jz").val(),
            },
            dataType: "json",
            json: "callback",
            timeout: 60000,
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    res.result.inName = localStorage.getItem("user_name")
                    $alert("称重成功", 1400);
                    setTimeout(function () {
                        window.location.href = "warehouseWeigh.html";
                    }, 1300);
                    // 打印 钢丝绳称重标签
                    if (res.result.isPrint) {
                        takeupPrint("hdProduct", res.result);
                    }
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                if (error.status == 0) {
                    loading_hide();
                    $alert("请求超时，请重新提交！");
                } else {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    });

    $(".content").on("input", "input.pz", function () {
        setCpjz();
    });
    $(".content").on("input", "input.mz", function () {
        setCpjz();
    });

    //计算 净重
    function setCpjz() {
        var mz = $(".content input.mz").val();
        var pz = $(".content input.pz").val();
        if (mz == "") {
            return;
        } else {
            if (pz == "") {
                pz = 0;
            }
            $(".content input.jz").val(accSub(mz, pz));
        }
    }


});

